package com.ruoyi.client.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.bookbus.domain.BookFavorite;
import com.ruoyi.bookbus.domain.BookProduct;
import com.ruoyi.client.mapper.FavoriteMapper;
import com.ruoyi.client.pojo.form.FavoriteAddForm;
import com.ruoyi.client.pojo.vo.FavoriteVo;
import com.ruoyi.client.service.FavoriteService;
import com.ruoyi.client.service.GoodsService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("ClientFavoriteService")
public class FavoriteServiceImpl implements FavoriteService {

    @Resource(name = "ClientFavoriteMapper")
    private FavoriteMapper favoriteMapper;

    @Resource
    private GoodsService goodsService;

    @Override
    public Boolean add(FavoriteAddForm form) {
        Long userId = requireLogin();
        BookProduct product = goodsService.selectByPrimaryKey(form.getProductId());
        if (product == null) {
            throw new ServiceException("商品不存在");
        }

        BookFavorite existing = favoriteMapper.selectByUserIdAndProductId(userId, form.getProductId());
        if (existing != null) {
            return true;
        }

        BookFavorite favorite = new BookFavorite();
        favorite.setUserId(userId);
        favorite.setProductId(form.getProductId());
        // 设置商品信息
        // 清理图片URL中的换行符和空白字符
        String pic = product.getPic();
        if (pic != null) {
            pic = pic.trim().replaceAll("[\\r\\n]+", "");
        }
        favorite.setPic(pic);
        favorite.setTitle(product.getTitle());
        favorite.setAuthor(product.getAuthor());
        favorite.setPrice(product.getPrice());
        favorite.setOriginalPrice(product.getOriginalPrice());
        return favoriteMapper.insert(favorite) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        Long userId = requireLogin();
        BookFavorite favorite = favoriteMapper.selectByPrimaryKey(id);
        if (favorite == null) {
            throw new ServiceException("收藏不存在");
        }
        if (!favorite.getUserId().equals(userId)) {
            throw new ServiceException("无权操作该收藏");
        }
        return favoriteMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public Boolean deleteByProductId(Long productId) {
        Long userId = requireLogin();
        BookProduct product = goodsService.selectByPrimaryKey(productId);
        if (product == null) {
            throw new ServiceException("商品不存在");
        }
        return favoriteMapper.deleteByUserIdAndProductId(userId, productId) > 0;
    }

    @Override
    public Boolean deleteAllByMy() {
        Long userId = requireLogin();
        return favoriteMapper.deleteByUserId(userId) > 0;
    }

    @Override
    public List<FavoriteVo> listByMy() {
        Long userId = requireLogin();
        return favoriteMapper.listByUserId(userId);
    }

    @Override
    public Boolean isFavorite(Long productId) {
        if (!StpUtil.isLogin()) {
            return false;
        }
        Long userId = StpUtil.getLoginIdAsLong();
        BookFavorite favorite = favoriteMapper.selectByUserIdAndProductId(userId, productId);
        return favorite != null;
    }

    private Long requireLogin() {
        if (!StpUtil.isLogin()) {
            throw new ServiceException("用户未登录");
        }
        return StpUtil.getLoginIdAsLong();
    }
}

